﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;

namespace HP.Cachalote.WPF.Shared
{
    public class UserControlBase : UserControl
    {
        public UserControlBase()
        {
            RegisterMessages();

            Unloaded += new RoutedEventHandler(UserControlBase_Unloaded);
        }

        /// <summary>
        /// Register all necessary messages.
        /// </summary>
        protected virtual void RegisterMessages() { }

        protected virtual void UserControlBase_Unloaded(object sender, RoutedEventArgs e)
        {
            // Cleanup in View.
            Messenger.Default.Unregister(this);

            // Cleanup in ViewModel.
            var viewModel = DataContext as MyViewModelBase;

            if (viewModel != null)
            {
                viewModel.Cleanup();
            }
        }
    }
}
